﻿using Dao;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Web_Ordering : System.Web.UI.Page
{

    public string OrderInfor { get; set; }
    private OrderDao OrderDao = new OrderDao();
    private Account account ;

    protected void Page_Load(object sender, EventArgs e)
    {
        // 验证当前是否有用户登录
        account = (Account)Session["account"];
        // 未登录则禁止访问本页，跳转到登录页面
        if (account == null)
        {
            Response.Redirect("Login.aspx");
        }
        else
        {

            // 通过用户ID获取订单
            Order order = (Order)Session["orderInfo"];

                ReflashPage(order);
        }
    }


    protected void BtnAddOrder_Click(object sender, EventArgs e) {

        Order order = (Order)Session["orderInfo"];

        if (OrderDao.AddNewOrder(order))
        {
            Response.Redirect("Order.aspx");
        }
        else 
        {
            EmptyInfo.Text = "请求失败";
            //Response.Redirect("Cart.aspx");
        };
    }



    protected void ReflashPage(Order order)
    {
        if (order != null)
        {
            // 套接字符串
            StringBuilder sbBuilder = new StringBuilder();

            sbBuilder.AppendFormat(
                "<div class ='page_bookDetail' >" +
                "<div class='row'>" +
                " <div class='col-md-3 clean'><img alt = '商品预览图' class='book_img' src='../Book_Image/" + order.Book.Image + "' /></div>" +
                    "<div class='col-md-8'><div class='row'><div class='col-md-offset-1'>" +
                    "<h2>《" + order.Book.Name + "》</h2><h4><font color='#D5D5D5'>推荐理由：</font>" + order.Book.Oneword + "</h4><hr class='bg-info' />" +
                                "<blockquote class='bg-warning text-danger'>" +
                                   "<h3 class='text-primary'>金额：<font size = '5' color='#D9534F'>￥" + order.Book.Price + "</font ><font color='#D5D5D5' size='3'> x " + order.Count + "</font></h3>" +
                                   "<h3 class='text-primary'>用户名：<font size = '5' color='#D9534F'>" + account.Username + "</font></h3>" +
                                   "<h3 class='text-primary'>联系方式：<font size = '5' color='#D9534F'>" + account.Telephone + "</font></h3>" +
                                   "</blockquote>" +
                                "<div class='row'>" +
                                    "<div class='col-md-12'><font size = '5' > 合 计：</font><font size = '7' color='#D9534F'>" + order.Book.Price * order.Count + "</ font ></div>" +
                                    "<div class='col-md-12  float-right'>" +
                                    "<input type = 'text' style='display:none' id='bookId' value='" + order.Book.Id + "'/>" +
                                    "<input type = 'text' style='display:none' id='num' value='" + order.Count + "'/>" +
                                     "<input type = 'text' style='display:none' id='money' value='" + order.Book.Price * order.Count + "'/>" +
                                       "<input type = 'button' class='btn btn-danger' id='btnAddOrder' value='提交订单'/>" +
                                    "</div>" +
                                "</div>" +
                                "<br/>" +
                                "<div" +
                                    "<lable class='text-danger' id='errorInfo' value=''></label>" +
                                "</div>" +
                            "</div>" +
                        "</div>" +
                   " </div>" +
                "</div>" +
            "</div>");

            // 返回给浏览器
            OrderInfor = sbBuilder.ToString();
        }
        else
        {
            EmptyInfo.Text = "请求失败";
            //Response.Redirect("Cart.aspx");
        }
    }


}